//
//  WebDemoCtr.swift
//  soapp_Example
//
//  Created by zhuxietong on 2020/12/9.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import Foundation
import soapp
import Eelay


class WebDemoCtr:WebController{
    open var jsconfig:WebConfiguration{
        get{
            let config = WebConfiguration()
            config.initBlock = { hander,controller in
                controller.add(hander, name: "callApp")
                
                let user = JoUser.loginUser()
                let token = user?.token ?? ""
                let userid = user?.userid ?? ""
                let appInfo =
                """
                function getAppInfo(){
                window.localStorage.setItem('client','iosapp')
                return {'token':'\(token)','userid':'\(userid)','client':'iosapp'}
                }
                """
                
                let script11 = WKUserScript(source: appInfo, injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: true)
                let script12 = WKUserScript(source: appInfo, injectionTime: WKUserScriptInjectionTime.atDocumentEnd, forMainFrameOnly: true)
                controller.addUserScript(script11)
                controller.addUserScript(script12)
            }
            
            
            weak var wself = self
            config.receiveJsAction = {
                message,controller in
                
                let observers = WebController.JSObservers
                for one  in observers {
                    _ = one(message,wself)
                }
                
            }
            return config
        }
    }
    
}
